<?php
/***************************************************************************
   Coppermine 1.3.1 for CPG-Dragonfly™
  **************************************************************************
   Port Copyright (c) 2004-2005 CPG Dev Team
   http://dragonflycms.com/
  **************************************************************************
   v1.1 (c) by Grégory Demar http://coppermine.sf.net/
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
****************************************************************************/
if (!defined('CPG_NUKE')) { exit; }

define('ALBMGR_PHP', true);
require("modules/" . $module_name . "/include/load.inc");

//if (!(GALLERY_ADMIN_MODE || USER_ADMIN_MODE)) cpg_die(_ERROR, ACCESS_DENIED, __FILE__, __LINE__, 0, 1);
if  (!USER_CAN_CREATE_ALBUMS) {
	cpg_die(_ERROR, ACCESS_DENIED);
}

// init.inc: $cat   = $_POST->uint('cat')   ?: $_GET->uint('cat');
// init.inc: $album = $_POST->uint('album') ?: $_GET->uint('album');
if (USER_GAL_CAT == $cat) {
	$thisalbum = 'category > ' . FIRST_USER_CAT;
} else if ($album) {
	$thisalbum= "a.aid = {$album}";
} else if ($cat) {
	$thisalbum = "category = {$cat}";
} else {
	$thisalbum = "category >= 0"; // just something that is true
}

function get_subcat_data($parent, $ident = '')
{
	global $CONFIG, $CAT_LIST, $db;
	$parent = intval($parent);
	$result = $db->query("SELECT cid, catname FROM {$CONFIG['TABLE_CATEGORIES']} WHERE parent = {$parent} AND cid != 1 ORDER BY pos");
	foreach ($result as $subcat) {
		$CAT_LIST[] = array($subcat['cid'], $ident . $subcat['catname']);
		get_subcat_data($subcat['cid'], $ident . '&nbsp;&nbsp;&nbsp;');
	}
}

global $THEME_DIR, $db;

if ((isset($_POST['aid']) && intval($_POST['aid'])>0 && isset($_POST['move'])) && ($_POST['move'] == 'up' || $_POST['move'] == 'top'))
{
	$album = $db->uFetchAssoc("SELECT title, pos, category FROM {$CONFIG['TABLE_ALBUMS']} WHERE aid = ".intval($_POST['aid']));
	if ($album['pos'] > 0) {
		$newpos = ($_POST['move'] == 'top') ? 0 : ($album['pos']-1);
		$db->exec("UPDATE {$CONFIG['TABLE_ALBUMS']} SET pos=pos+1 WHERE category = {$album['category']} AND pos < $album[pos] AND pos > $newpos-1");
		$db->exec("UPDATE {$CONFIG['TABLE_ALBUMS']} SET pos=$newpos WHERE aid = $_POST[aid]");
	}
	URL::redirect(URL::index("&file=albmgr&cat={$album['category']}"));
}

else if ((isset($_POST['aid']) && intval($_POST['aid'])>0 && isset($_POST['move'])) && ($_POST['move'] == 'down' || $_POST['move'] == 'bottom') )
{
	$album = $db->uFetchAssoc("SELECT pos, category FROM {$CONFIG['TABLE_ALBUMS']} WHERE aid = ".$_POST->uint('aid'));
	list($last) = $db->uFetchRow("SELECT pos FROM {$CONFIG['TABLE_ALBUMS']} WHERE category = {$album['category']} ORDER BY pos DESC");
	if ($album['pos'] < $last) {
		$newpos = ($_POST['move'] == 'down') ? ($album['pos']+1) : $last;
		$db->exec("UPDATE {$CONFIG['TABLE_ALBUMS']} SET pos=pos-1 WHERE category = {$album['category']} AND pos > {$album['pos']} AND pos < {$newpos}+1");
		$db->exec("UPDATE {$CONFIG['TABLE_ALBUMS']} SET pos={$newpos} WHERE aid = ".$_POST->uint('aid'));
	}
	URL::redirect(URL::index("&file=albmgr&cat={$album['category']}"));
}

else if ((isset($_POST['mode']) && $_POST['mode'] == 'addalb') && isset($_POST['cat']))
{
	if (GALLERY_ADMIN_MODE) {
		$cat = intval($_POST['cat']);
	} else {
		$cat = FIRST_USER_CAT + USER_ID;
	}
	list($last) = $db->uFetchRow("SELECT pos FROM {$CONFIG['TABLE_ALBUMS']} WHERE category = {$cat} ORDER BY pos DESC");
	if (!$last) $last = 0;
	else ++$last;
	$title = Fix_Quotes($_POST['title']);
	if ($title == '') cpg_die(_ERROR, 'Album title can\'t be empty', __FILE__, __LINE__, 0, 1);
	$db->exec("INSERT INTO {$CONFIG['TABLE_ALBUMS']} (title, pos, category, description) VALUES ('$title', '$last', '$cat', '')");
	URL::redirect(URL::index("&file=albmgr&cat=$cat"));
}

/*if ((isset($_POST['mode']) && $_POST['mode'] == 'delalb') && intval($_POST['aid']) > 0) {
	$message = CONFIRM_DELETE1.'<br /><br />'.CONFIRM_DELETE2.'<br />
	<a href="'.URL::index('&amp;file=delete&amp;id='.intval($_POST['aid']).'&amp;what=album').'">'.YES.'</a> / <a href="javascript:history.go(-1)">'.NO.'</a>';
	cpg_die('Delete album', $message, __FILE__, __LINE__, 0, 1);
}*/

$cat = intval($_POST->uint('cat') ?: $_GET->uint('cat'));
if ($cat == 1) $cat = 0;

if (GALLERY_ADMIN_MODE) {
	$result = $db->query("SELECT aid, title, pos, description, thumb FROM {$CONFIG['TABLE_ALBUMS']} WHERE category = {$cat} ORDER BY pos ASC");
} else {
	$result = $db->query("SELECT aid, title, pos, description, thumb FROM {$CONFIG['TABLE_ALBUMS']} WHERE category = " . (USER_ID + FIRST_USER_CAT) . " ORDER BY pos ASC");
}/*else cpg_die(_ERROR, PERM_DENIED);
*/

pageheader(ALB_MRG);
starttable("100%", ALB_MRG, 4);
echo '<tbody>';

// Create category select dropdown
if (GALLERY_ADMIN_MODE) {
	$CAT_LIST = array();
	$CAT_LIST[] = array(FIRST_USER_CAT + USER_ID, MY_GALLERY);
	$CAT_LIST[] = array(0, NO_CATEGORY);
	get_subcat_data(0, '');

	echo '<tr>
		<td colspan="4" style="text-align:center"><form name="album_menu" method="post" action="'.URL::index('&amp;file=albmgr',0,1).'">
			<input type="hidden" name="name" value="'.$module_name.'" />
			<input type="hidden" name="file" value="albmgr" />
			<select name="cat" class="listbox">';
	foreach($CAT_LIST as $catory) {
		echo '<option value="' . $catory[0] . '"' . ($cat == $catory[0] ? ' selected="selected"': '') . ">" . $catory[1] . "</option>\n";
	}
	echo '</select>
		<input type="submit" class="button" value="'.SELECT_CATEGORY.'" /></form>
		</td>
	</tr>';
}


// Now let's create the list of albums that belong to the choosen category
if ($result->num_rows)
{
	list($last) = $db->uFetchRow("SELECT pos FROM {$CONFIG['TABLE_ALBUMS']} WHERE category = {$cat} ORDER BY pos DESC");
	foreach ($result as $album)
	{
		$img = '<img src="'.$THEME_DIR.'/images/nopic.jpg" alt="" />';
		if ($album['thumb'] > 0) {
			$picture = $db->uFetchAssoc("SELECT filepath, filename, pwidth, pheight FROM {$CONFIG['TABLE_PICTURES']} WHERE pid='$album[thumb]'");
			$img = '<a onclick="window.open(\''.URL::index('&file=displayimagepopup&pid='.$album['thumb'].'&fullsize=1').'\',\'preview\',\'resizable=yes,scrollbars=yes,width='.($picture['pwidth']+30).',height='.($picture['pheight']+40).',left=0,top=0\');return false" target="preview" href="'.URL::index('&amp;file=displayimagepopup&amp;pid='.$album['thumb'].'&amp;fullsize=1').'"><img src="'.get_pic_url($picture, 'thumb').'" alt=""  height="75" border="0" /></a>';
		}
		list($count) = $db->uFetchRow("SELECT COUNT(*) FROM {$CONFIG['TABLE_PICTURES']} WHERE aid='$album[aid]'");
		/*if ($count > 0) {
			$count .= '<br /><a href="'.URL::index('&amp;file=editpics&amp;album='.$album['aid']).'">Edit pictures</a>';
		}*/
		$MOD_ALB = str_replace(' my', '',MODIFYALB_LNK);
		$MOD_ALB = str_replace('s', '',$MOD_ALB);
		echo '<tr>
			<td rowspan="2" width="100">'.$img.'</td>
			<td class="tableh1"><b>'.$album['title'].'</b></td>
			<td colspan="2" class="tableh1" style="text-align:center"><b><span class="statlink">'.OPERATIONS.'</span></b></td>
		</tr>
		<tr>
			<td valign="top">'.$album['description'].'<br />
				<a class="edit" href="'.URL::index("&amp;file=editpics&amp;album={$album['aid']}").'" title="'.EDIT_PICS.'">'.EDIT_PICS.'</a>
			</td>
			<td style="width:100px">
				<a class="edit" href="'.URL::index("&amp;file=modifyalb&amp;id={$album['aid']}").'"></a>
				<form style="display:inline" method="post" action="'.URL::index("&amp;file=delete",1,1).'">
					<input type="hidden" name="what" value="album" />
					<input type="hidden" name="id" value="'.$album['aid'].'" />
					<button type="submit" class="delete" title="'.DELETE.' '.ALBUM.'"></button>
				</form>';
		if ($album['pos'] > 0) {
				echo '
				<form style="display:inline" method="post" action="">
					<input type="hidden" name="move" value="top" />
					<input type="hidden" name="aid" value="'.$album['aid'].'" />
					<button type="submit" class="top"></button>
				</form>
				<form style="display:inline" method="post" action="">
					<input type="hidden" name="move" value="up" />
					<input type="hidden" name="aid" value="'.$album['aid'].'" />
					<button type="submit" class="up"></button>
				</form>';
		}
		echo'</td><td style="width:50px">';
		if ($album['pos'] < $last) {
			echo '
				<form style="display:inline" method="post" action="">
					<input type="hidden" name="move" value="down" />
					<input type="hidden" name="aid" value="'.$album['aid'].'" />
					<button class="down" type="submit"></button>
				</form>
				<form style="display:inline" method="post" action="">
					<input type="hidden" name="move" value="bottom" />
					<input type="hidden" name="aid" value="'.$album['aid'].'" />
					<button type="submit" class="bottom"></button>
				</form>';
		}
		echo "</td></tr>\n\n";
	}
} else {
	echo '<tr><td colspan="4" style="text-align:center" height="75"><b>No albums in this category</b></td></tr>';
}
echo '
</tbody><tfoot>
	<tr><td colspan="4">
		<form name="new_album" method="post" action="'.URL::index('&amp;file=albmgr').'">
		<input type="hidden" name="cat" value="'.$cat.'" />
		<input type="hidden" name="mode" value="addalb" />
		<input type="text" name="title" size="27" maxlength="80" /> <input type="submit" class="button" value="'.NEW_ALBUM.'" />
	</td></tr>
</tfoot>
</form>';
endtable();
pagefooter();
